<?php

namespace App\Console\Commands\Shop;

use App\Kuafu\Nsq\Enum\NsqCodeEnum;
use App\Kuafu\Shop\Enum\CodeEnum;
use App\Kuafu\Shop\Model\Shop;
use App\Kuafu\Shop\Service\ShopAttributeService;
use Illuminate\Console\Command;

class SyncShopAttributeByInit extends Command
{
    protected $signature = 'shop:syncShopAttributeByInit';

    protected $description = '初始化门店属性后同步到tiger筹备信息中';

    public function handle()
    {
        Shop::query()
            ->select(['id', 'name'])
            ->whereIn('status', [CodeEnum::SHOP_STATUS_NORMAL, CodeEnum::SHOP_STATUS_PREPARE])//启用+筹备中
            ->chunkById(500, function ($shops) {
                $shopIds           = array_column($shops->toArray(), 'id');
                $shopAttributeData = ShopAttributeService::getShopAttribute($shopIds);
                foreach ($shops as $shop) {
                    if (isset($shopAttributeData[$shop['id']]) &&
                        ($shopAttributeData[$shop['id']]['menuLevel'] || $shopAttributeData[$shop['id']]['sellingCategory'])) {

                        dump($shop['id'], $shop['name']);

                        $data['menuLevel']       = $shopAttributeData[$shop['id']]['menuLevel'] ?? 0;// 产品定价 菜单等级
                        $data['sellingCategory'] = $shopAttributeData[$shop['id']]['sellingCategory'] ?
                            implode(",", $shopAttributeData[$shop['id']]['sellingCategory']) : '';// 售卖品类


                        (new \Kuafu\Nsq\NsqPublisher(config('nsq.default.address')))
                            ->pub(NsqCodeEnum::SHOP_ATTRIBUTE_EVENT, [
                                'shopId' => $shop['id'],
                                'data'   => $data,
                            ]);
                    }
                }
            });


    }

}
